﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Nancy;
using System.IO;
using System.Configuration;
using PlatWL.Data;
using Nancy.ModelBinding;
using PlatWL.Services.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using PlatWL.Services.Other;

namespace PlatWL.Modules.Other
{
    public class WeixinModule : SimpleModule
    {
        public WeixinModule(WeixinManage service) : base("weixin")
        {
            //JSAPI权限
            Post["/jsapi"] = p =>
            {
                string url = HttpContext.Current.Server.UrlDecode(Request.Form["url"]);

                service.JSAPI(ref msg, url);
                return Response.AsJson(msg);
            };

            //根据code获取授权用户信息,并生成登录授权码
            Get["/code/{code}"] = p =>
            {
                string refstr = this.Request.Query["ref"];
                service.GetUserInfoByCode(ref msg, p.code, refstr ?? "");
                return Response.AsJson(msg);
            };

            //微信支付参数
            Get["/payjsapi/{orderid:int}/{code}"] = p =>
            {
                service.GetPayJSAPI(ref msg, "JSAPI", p.orderid, p.code);
                return Response.AsJson(msg);
            };
            //H5支付
            Get["/h5api/{orderid:int}"] = p =>
            {
                service.GetPayJSAPI(ref msg, "MWEB", p.orderid, "");
                return Response.AsJson(msg);
            };
            //微信支付异步通知
            Post["/paynotifyurl"] = p =>
            {
                service.PayNotifyUrl(ref msg);
                string xml = msg.result;
                return xml;
            };

            //微信退款接口
            Get["/payjsapi/{orderid:int}"] = p =>
            {
                service.ReFund(ref msg, p.orderid);
                return Response.AsJson(msg);
            };

            Get["/wx"] = p =>
            {

                string signature = this.Request.Query["signature"];
                string timestamp = this.Request.Query["timestamp"];
                string nonce = this.Request.Query["nonce"];
                string echostr = this.Request.Query["echostr"];

                if (service.Check(ref msg, signature, timestamp, nonce))
                {
                    return Response.AsText(echostr);
                }
                else
                {
                    return Response.AsText("error");
                }

            };
            Post["/wx"] = p =>
            {

                return Response.AsText("");
            };


        }
    }

}